package ch5_1.StaticBroadcast.app.src.main.java.me.learningandroid.ch5_1.staticbroadcastdemo;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

public class CallStartedReceiver extends BroadcastReceiver {
    private static final String SPREF_BOOTCOUNT="SPREF_BOOTCOUNT";

    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
        int bootCount = sharedPreferences.getInt(SPREF_BOOTCOUNT,0);
        bootCount += 1;
        sharedPreferences.edit()
                .putInt(SPREF_BOOTCOUNT,bootCount)
                .apply();
        Toast.makeText(context, String.format("监听重启%s次", bootCount), Toast.LENGTH_LONG).show();
        /*
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("id1", "channel", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(1, new NotificationCompat.Builder(context, "id1")
                .setContentTitle(String.format("监听到电话%s次", bootCount))
                .setContentText(String.format("监听到电话%s次", bootCount))
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0))
                .build());

         */
    }
}